package main

import (
	_ "beegoProject12/models/auth"
	_ "beegoProject12/routers"
	"beegoProject12/utils"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	"log"
)

/*
9.65【权限管理模块】角色上配置权限五
	1. role-auth-add.html 94H,195H,43H,166H,
	2. role_controller.go DoRoleAuth
	3. 路由

9.66【权限管理模块】菜单动态化过滤用户（不同到用户显示不同的菜单）
	1. home_controller.go Get

9.67【权限管理模块】权限管理模块小结
	1. home_controller.go 84H
	2. index.html 30H
*/

func init() {
	user := beego.AppConfig.DefaultString("mysql::Username", "root")
	pwd := beego.AppConfig.DefaultString("mysql::Password", "Ideal123")
	host := beego.AppConfig.DefaultString("mysql::Host", "127.0.0.1")
	port := beego.AppConfig.DefaultInt("mysql::Port", 3306)
	dbName := beego.AppConfig.DefaultString("mysql::DbName", "beegoProject")

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=true&loc=PRC",
		user, pwd, host, port, dbName)

	orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.RegisterDataBase("default", "mysql", dsn)

	db, err := orm.GetDB("default")
	if err != nil {
		log.Fatal(err)
	} else if err := db.Ping(); err != nil {
		log.Fatal(err)
	}
}

func main() {
	orm.RunCommand()
	/*	orm.Debug = true*/
	beego.BConfig.WebConfig.Session.SessionOn = true

	// 未登录请求拦截
	beego.InsertFilter("/main/*", beego.BeforeRouter, utils.LoginFilter)

	// 日志
	logs.SetLogger(logs.AdapterMultiFile, `{"filename":"logs/beego.log","separate":["error","info","debug"]}`)

	beego.Run()
}
